﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Acr.UserDialogs;
using Cashbook.Models;
using Cashbook.Services;
using Cashbook.ViewModels;
using Xamarin.Forms;

namespace Cashbook.Views
{
    public partial class EditBillPage : ContentPage
    {
        //public Bill Bill { get; set; }
		

        public EditBillVM editBillVM;

		public EditBillPage(int billId)
		{
			
			InitializeComponent();
			
            editBillVM = new EditBillVM(billId,this.Navigation, UserDialogs.Instance);
		}

		protected override void OnAppearing()
		{
			base.OnAppearing();

            if (editBillVM.Customers.Count == 0 || editBillVM.Bill == null)
				editBillVM.LoadBillCommand.Execute(null);
			// The instance of CustomersDataAccess
			// is the data binding source
			//Customers = await _customerService.GetCustomersOC();
       //     if(Bill.CustomerId>0)
			    //SelectedCustomer = Customers.FirstOrDefault(c => c.Id == Bill.CustomerId);

			BindingContext = editBillVM;
		}

		//async void OnAddClick(object sender, EventArgs e)
		//{
		//	if (string.IsNullOrEmpty(titleEntry.Text))
		//	{
		//		await DisplayAlert("错误", "请输入项目主题", "关闭");
		//		titleEntry.Focus();
		//		return;
		//	}
		//	if (string.IsNullOrEmpty(amountEntry.Text))
		//	{
		//		await DisplayAlert("错误", "请输入金额", "关闭");
		//		amountEntry.Focus();
		//		return;
		//	}
		//	int amount;
		//	int.TryParse(amountEntry.Text, out amount);
		//	if (amount <= 0)
		//	{
		//		await DisplayAlert("错误", "请输入金额必须大于0", "关闭");
		//		amountEntry.Focus();
		//		return;
		//	}

		//	//if (string.IsNullOrEmpty(clientNameEntry.Text))
		//	//{
		//	//	await DisplayAlert("错误", "请输入客户", "关闭");
		//	//	clientNameEntry.Focus();
		//	//	return;
		//	//}




  //          await this.dataAccess.SaveBillAsync(Bill);


  //          //MessagingCenter.Send(this, "AddItem", bill);
  //          //await Navigation.PopToRootAsync();
  //          if(Bill.IsPayed)
  //              await Navigation.PushAsync(new BillIsPayedPage());
  //          else{
  //              await Navigation.PushAsync(new BillPage());
  //          }
		//}

		void Handle_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (picker.SelectedIndex == -1)
			{
			    DisplayAlert("错误", "请选择客户", "关闭");
			}
			else
			{
                //SelectedCustomer = (Customer)picker.Items[picker.SelectedIndex];
                editBillVM.Bill.CustomerId = editBillVM.SelectedCustomer.Id;
			}
		}

	
    }
}
